package com.southwind.springboottest.study.Java中通讯.socket通讯;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        String host = "localhost"; // 服务器地址
        int port = 12345; // 服务器端口

        try (Socket socket = new Socket(host, port)) {
            // 获取输入流和输出流
            InputStream input = socket.getInputStream();
            OutputStream output = socket.getOutputStream();

            // 创建输入流和输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter writer = new PrintWriter(output, true);

            // 读取用户输入并发送到服务器
            System.out.println("Enter message to send to server:");
            String message = reader.readLine();
            writer.println(message);

            // 读取服务器回复
            BufferedReader responseReader = new BufferedReader(new InputStreamReader(input));
            String response = responseReader.readLine();
            System.out.println("Server response: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

